package com.du.stutestsys.serverImpl.AdministratorImpl;

import com.du.stutestsys.dao.SysUserMapper;
import com.du.stutestsys.service.Administrator.SysUserService;
import com.du.stutestsys.util.DuUtil;
import com.du.stutestsys.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.du.stutestsys.entity.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public SysUser Login(SysUser sysUser) {
        return sysUserMapper.Login(sysUser);
    }

    @Override
    public boolean addAmin(SysUser sysUser) {
        return sysUserMapper.addUser(sysUser)>0;
    }

    @Override
    @Transactional
    public boolean deleteAdmin(List<SysUser> sysUserList) {
//        for (SysUser user : sysUserList) {
//            //通过id 获取当前id的type权限,如果type = 0 则不让他进入SQL
//            if (user.getType()!=null){
//                if (user.getType().equals("0")) {
//                    sysUserList.remove(user);
//                }
//            }
//        }
        return sysUserMapper.deleteAdmin(sysUserList)>0;
    }

    @Override
    public boolean updateAdmin(SysUser sysUser) {
        Integer integer = sysUserMapper.updateAdmin(sysUser);
        System.out.println(integer);
        return integer>0;
    }

    @Override
    public List<SysUser> selectAdmin(SysUser sysUser, PageUtil pageUtil) {
        if (sysUser.getSubjectId()!=null){
            Long teacherid = sysUserMapper.selectSubId(sysUser.getSubjectId());
            sysUser.setId(teacherid);
        }
        return sysUserMapper.selectUser(sysUser,pageUtil);
    }

    @Override
    public boolean updatePrivilegeAdmin(List<SysUser> sysUserList) {
        return sysUserMapper.updatePrivilegeAdmin(sysUserList)>0;
    }

    @Override
    @Transactional
    public boolean addStuUser(SysUser stuUser) {
        // 如果是自己写的接口则不会回填自增id
//        boolean add1 = sysUserMapper.addUser(stuUser)>0;
        boolean add1 = sysUserMapper.insert(stuUser)>0;

        System.out.println(stuUser.getClass_id());
        boolean add2 = sysUserMapper.addStu(stuUser.getId(),stuUser.getClass_id())>0;
        return DuUtil.isAdds(new Boolean[]{add1,add2});
    }

    @Override
    @Transactional
    public boolean deleteStu(List<Long> stuids) {
        //删除学生角色表

        boolean add1 = sysUserMapper.deleteBatchIds(stuids)>0;
        //删除学生
        boolean add2 = sysUserMapper.deleteStu(stuids)>0;
        return DuUtil.isAdds(new Boolean[]{add1,add2});
    }

    @Override
    @Transactional
    public boolean updateTeaSub(SysUser sysUser) {
        boolean add1 = sysUserMapper.updateAdmin(sysUser)>0;
        //删除学生
        boolean add2 = sysUserMapper.updateTeaSub(sysUser)>0;
        return DuUtil.isAdds(new Boolean[]{add1,add2});
    }

}
